package com.dhms.vostok1.data.message_center

import com.squareup.moshi.Json

//消息任务状态
enum class MessageTaskStateType(val value: String) {
    //任务生成
    TASK_CREATE("task_create"),

    //任务进行时
    TASK_START("task_start"),

    //任务取消
    TASK_CANCEL("task_cancel"),

    //任务结束前10分钟
    TASK_END_OF_10("task_end_of_10"),

    //任务完成
    TASK_COMPLETE("task_complete"),

    //任务超时
    TASK_OVERTIME("task_overtime"),

    //驳回通知
    NOTIFY_FLOW_REJECTED("notify_flow_rejected"),

    //通过通知
    NOTIFY_FLOW_APPROVED("notify_flow_approved"),

    //撤销通知
    NOTIFY_FLOW_REVOKED("notify_flow_revoked"),

    //申请人 撤销
    NOTIFY_INITIATOR_FLOW_REVOKED("notify_initiator_flow_revoked")
}


//审批消息类型
enum class MessageApprovalType(val value: String) {
    UNKNOWN(""),

    //采购单流转通知
    PURCHASE_ORDER_NOTIFY_NEXT_NODE("purchase_order:notify_next_node"),

    //采购单驳回通知
    PURCHASE_ORDER_NOTIFY_FLOW_REJECTED("purchase_order:notify_flow_rejected"),

    //采购单审批通过通知 【申请人】
    PURCHASE_ORDER_NOTIFY_FLOW_APPROVED("purchase_order:notify_flow_approved"),

    //采购单审批通过通知 【抄送人】
    PURCHASE_ORDER_NOTIFY_FLOW_CC_APPROVED("purchase_order:notify_flow_cc_approved"),

    //采购单审批撤销通知
    PURCHASE_ORDER_NOTIFY_FLOW_REVOKED("purchase_order:notify_flow_revoked"),

    //申请人 撤销 采购申请
    PURCHASE_ORDER_NOTIFY_INITIATOR_FLOW_REVOKED("purchase_order:notify_initiator_flow_revoked"),

    //入库单流转通知
    WAREHOUSING_ORDER_NOTIFY_NEXT_NODE("warehousing_order:notify_next_node"),

    //入库单审批通过通知 [申请人]
    WAREHOUSING_ORDER_NOTIFY_FLOW_APPROVED("warehousing_order:notify_flow_approved"),

    //入库单审批通过通知 [抄送人]
    WAREHOUSING_ORDER_NOTIFY_FLOW_CC_APPROVED("warehousing_order:notify_flow_cc_approved"),

    //入库单审批驳回通知
    WAREHOUSING_ORDER_NOTIFY_FLOW_REJECTED("warehousing_order:notify_flow_rejected"),

    //入库单审批撤销通知
    WAREHOUSING_ORDER_NOTIFY_FLOW_REVOKED("warehousing_order:notify_flow_revoked"),

    //申请人 撤销 入库申请
    WAREHOUSING_ORDER_NOTIFY_INITIATOR_FLOW_REVOKED("warehousing_order:notify_initiator_flow_revoked"),


    //TODO:  ??????

    // 申请人提交归还申请
    RETURN_ORDER_NOTIFY_NEXT_NODE("return_order:notify_next_node"),

    // 归还申请审批拒绝
    RETURN_ORDER_NOTIFY_FLOW_REJECTED("return_order:notify_flow_rejected"),

    // 您提交的归还申请已通过 [申请人]
    RETURN_ORDER_NOTIFY_FLOW_APPROVED("return_order:notify_flow_approved"),

    // 您提交的归还申请已通过 [抄送人]
    RETURN_ORDER_NOTIFY_FLOW_CC_APPROVED("return_order:notify_flow_cc_approved"),

    // 撤销归还申请 [申请人 撤销]
    RETURN_ORDER_NOTIFY_FLOW_REVOKED("return_order:notify_flow_revoked"),

    // 归还单撤销-发起人通知
    RETURN_ORDER_NOTIFY_INITIATOR_FLOW_REVOKED("return_order:notify_initiator_flow_revoked"),

    //出库单确认通知
    WAREHOUSE_OUT_ORDER_NOTIFY_FLOW_APPROVED("warehouse_out_order:notify_flow_approved"),

    //出库单作废通知
    WAREHOUSE_OUT_ORDER_NOTIFY_FLOW_REVOKED("warehouse_out_order:notify_flow_revoked"),

    //申领单流转通知
    RECIPIENTS_SUBMITTED_ID("recipients_order:notify_next_node"),

    //申领单驳回通知
    RECIPIENTS_SOMEONE_REJECTED_ID("recipients_order:notify_flow_rejected"),

    //申领单审批通过通知
    RECIPIENTS_ALL_APPROVED_ID("recipients_order:notify_flow_approved"),

    //申领单审批通过-抄送人通知
    RECIPIENTS_CC_APPROVED_ID("recipients_order:notify_flow_cc_approved"),

    //申领单审批撤销通知
    RECIPIENTS_REVOKED_ID_APPROVE("recipients_order:notify_flow_revoked"),

    //申领单撤销-发起人通知
    RECIPIENTS_REVOKED_ID_APPLICANT("recipients_order:notify_initiator_flow_revoked");


    companion object {
        fun fromString(type: String) =
            values().associateBy(MessageApprovalType::value)[type]
                ?: UNKNOWN
    }
}


//消息筛选用到的类型
enum class MessageFilterType(val value: String) {

    //采购
    PURCHASE_ORDER("purchase_order"),

    //入库
    WAREHOUSING_ORDER("warehousing_order"),

    //检修计划
    REPAIR_PLAN("repair_plan"),

    //检修验收
    REPAIR_APPROVAL("repair_approval")
}

//检修任务类型
enum class MessageRepairTaskType(val value: String) {
    UNKNOWN(""),

    //检修计划审批通知(1/2)
    REPAIR_APPROVAL_PLAN_APPROVAL("repair_approval:plan_approval"),

    //检修计划撤销通知-审批人(3)
    REPAIR_APPROVAL_PLAN_REVOKE_OTHER("repair_approval:plan_revoke_other"),

    // 检修计划撤销通知-申请人(4)
    REPAIR_APPROVAL_PLAN_REVOKE_APPLICANT("repair_approval:plan_revoke_applicant"),

    // 检修计划审批通过通知(5) 【申请人】
    REPAIR_APPROVAL_PLAN_APPROVAL_PASS("repair_approval:plan_approval_pass"),

    // 检修计划审批通过通知(5) 【抄送人】
    REPAIR_APPROVAL_PLAN_APPROVAL_PASS_CC("repair_approval:plan_approval_pass_cc"),

    // 检修计划驳回通知(6)
    REPAIR_APPROVAL_PLAN_APPROVAL_REFUSE("repair_approval:plan_approval_refuse"),

    // 检修任务生成通知分配执行人（任务开始前一周通知分配执行人）(7/12)
    REPAIR_TASK_CREATE_ASSIGN("repair:task_create_assign"),

    // 检修任务分配新执行人（任务开始前三天）(8/10/13)
    REPAIR_TASK_ASSIGN_EXECUTOR("repair:task_assign_executor"),

    // 检修任务修改执行人-通知原执行人(9)
    REPAIR_TASK_UPDATE_EXECUTOR("repair:task_update_executor"),

    // 检修任务开始(11)
    REPAIR_TASK_START("repair:task_start"),

    // 检修任务取消(14)
    REPAIR_TASK_CANCEL("repair:task_cancel"),

    // 检修任务验收流转通知(15/16)
    REPAIR_APPROVAL_TASK_APPROVAL("repair_approval:task_approval"),

    // 检修任务验收撤销通知审批人(17)
    REPAIR_APPROVAL_TASK_APPROVAL_CANCEL_OTHER("repair_approval:task_approval_cancel_other"),

    // 检修任务验收撤销通知申请人(18)
    REPAIR_APPROVAL_TASK_APPROVAL_CANCEL_APPLICANT("repair_approval:task_approval_cancel_applicant"),

    // 检修任务验收通过通知(19) [申请人]
    REPAIR_APPROVAL_TASK_APPROVAL_PASS("repair_approval:task_approval_pass"),

    // 检修任务验收通过通知(19) [抄送人]
    REPAIR_APPROVAL_TASK_APPROVAL_PASS_CC("repair_approval:task_approval_pass_cc"),

    // 检修任务验收驳回通知(20)
    REPAIR_APPROVAL_TASK_APPROVAL_REFUSE("repair_approval:task_approval_refuse");


    companion object {
        fun fromString(type: String) =
            values().associateBy(MessageRepairTaskType::value)[type]
                ?: UNKNOWN
    }
}


/**
 *
 * @param title  消息标题
 * @param messageTime  消息时间
 * @param messageType 消息类型
 * @param isRead  是否阅读 0未读，1已读
 * @param body
 * @param messageId 消息id
 */

data class MessageItemData(
    /*  消息标题 */
    @Json(name = "title")
    var title: String?,
    /*  消息时间 */
    @Json(name = "message_time")
    var messageTime: String?,
    /* 消息类型 */
    @Json(name = "message_type")
    var messageType: String?,
    /*  是否阅读 0未读，1已读 */
    @Json(name = "is_read")
    var isRead: Int?,
    @Json(name = "body")
    var body: MessageBodyData?,
    /* 消息id */
    @Json(name = "message_id")
    var messageId: Int,
    @Json(name = "group")
    var group: MessageCenterType?
) {

    //是否采购单
    private val isPurchaseOrder: Boolean
        get() {
            messageType?.let {
                return it.contains(MessageFilterType.PURCHASE_ORDER.value)
            }
            return false
        }

    val messageOne: String
        get() {
            return when (group) {
                //审批里含有检修，检修和审批要合并到一起，进行message_type判断
                MessageCenterType.APPROVAL,
                MessageCenterType.REPAIR,
                -> {
                    messageType?.let {
                        val approvalType = MessageApprovalType.fromString(it)
                        if (approvalType != MessageApprovalType.UNKNOWN) {
                            when (approvalType) {
                                MessageApprovalType.RETURN_ORDER_NOTIFY_FLOW_REJECTED,
                                MessageApprovalType.RETURN_ORDER_NOTIFY_FLOW_APPROVED,
                                MessageApprovalType.RETURN_ORDER_NOTIFY_FLOW_CC_APPROVED,
                                MessageApprovalType.RETURN_ORDER_NOTIFY_NEXT_NODE,
                                MessageApprovalType.RETURN_ORDER_NOTIFY_FLOW_REVOKED,
                                MessageApprovalType.RETURN_ORDER_NOTIFY_INITIATOR_FLOW_REVOKED,
                                -> {
                                    return "申请部门：${body?.applyDepartment ?: "--"}"
                                }
                                MessageApprovalType.RECIPIENTS_SOMEONE_REJECTED_ID,
                                MessageApprovalType.RECIPIENTS_ALL_APPROVED_ID,
                                MessageApprovalType.RECIPIENTS_CC_APPROVED_ID,
                                MessageApprovalType.RECIPIENTS_SUBMITTED_ID,
                                MessageApprovalType.RECIPIENTS_REVOKED_ID_APPROVE,
                                MessageApprovalType.RECIPIENTS_REVOKED_ID_APPLICANT,
                                -> {
                                    return "申领单位：${body?.applyDepartment ?: "--"}"
                                }
                                else -> {
                                    val content =
                                        if (isPurchaseOrder) body?.orderName else body?.purchaseOrderName
                                    return "采购计划：${content ?: "--"}"
                                }
                            }
                        } else {
                            when (MessageRepairTaskType.fromString(it)) {
                                MessageRepairTaskType.REPAIR_TASK_CREATE_ASSIGN,
                                MessageRepairTaskType.REPAIR_TASK_ASSIGN_EXECUTOR,
                                MessageRepairTaskType.REPAIR_TASK_UPDATE_EXECUTOR,
                                MessageRepairTaskType.REPAIR_TASK_START,
                                MessageRepairTaskType.REPAIR_TASK_CANCEL,
                                -> {
                                    return "设备名称：${body?.deviceName ?: "--"}"
                                }
                                else -> {
                                    return "检修计划：${body?.planName ?: "--"}"
                                }
                            }
                        }
                    }
                    ""
                }
                //出库
                MessageCenterType.WAREHOUSE_OUT -> {
                    return "申领单位：${body?.applyDepartment ?: "--"}"
                }
                else -> {
                    "设备名称：${body?.deviceName ?: "--"}"
                }
            }
        }

    val messageTwo: String
        get() {
            return when (group) {
                MessageCenterType.APPROVAL,
                MessageCenterType.REPAIR,
                -> {
                    messageType?.let {
                        val approvalType = MessageApprovalType.fromString(it)
                        if (approvalType != MessageApprovalType.UNKNOWN) {
                            when (approvalType) {
                                MessageApprovalType.RETURN_ORDER_NOTIFY_FLOW_REJECTED,
                                MessageApprovalType.RETURN_ORDER_NOTIFY_FLOW_APPROVED,
                                MessageApprovalType.RETURN_ORDER_NOTIFY_FLOW_CC_APPROVED,
                                MessageApprovalType.RETURN_ORDER_NOTIFY_NEXT_NODE,
                                MessageApprovalType.RETURN_ORDER_NOTIFY_FLOW_REVOKED,
                                MessageApprovalType.RETURN_ORDER_NOTIFY_INITIATOR_FLOW_REVOKED,
                                -> {
                                    return "归还类型：${body?.orderBizType ?: "--"}"
                                }
                                MessageApprovalType.RECIPIENTS_SOMEONE_REJECTED_ID,
                                MessageApprovalType.RECIPIENTS_ALL_APPROVED_ID,
                                MessageApprovalType.RECIPIENTS_CC_APPROVED_ID,
                                MessageApprovalType.RECIPIENTS_SUBMITTED_ID,
                                MessageApprovalType.RECIPIENTS_REVOKED_ID_APPROVE,
                                MessageApprovalType.RECIPIENTS_REVOKED_ID_APPLICANT,
                                -> {
                                    return "申领总量：${body?.orderTotalCount ?: "--"}"
                                }
                                else -> {
                                    val name = if (isPurchaseOrder) "计划类型" else "入库时间"
                                    val content =
                                        if (isPurchaseOrder) body?.planType else body?.warehouseTime
                                    return "$name：${content ?: "--"}"
                                }
                            }
                        } else {
                            when (MessageRepairTaskType.fromString(it)) {
                                MessageRepairTaskType.REPAIR_TASK_CREATE_ASSIGN,
                                MessageRepairTaskType.REPAIR_TASK_ASSIGN_EXECUTOR,
                                MessageRepairTaskType.REPAIR_TASK_UPDATE_EXECUTOR,
                                MessageRepairTaskType.REPAIR_TASK_START,
                                MessageRepairTaskType.REPAIR_TASK_CANCEL,
                                -> {
                                    return "关联计划：${body?.planName ?: "--"}"
                                }
                                else -> {
                                    return "检修类型：${body?.repairType ?: "--"}"
                                }
                            }
                        }
                    }
                    ""
                }

                //出库
                MessageCenterType.WAREHOUSE_OUT -> {
                    return "出库总量：${body?.orderTotalCount ?: "0"}"
                }
                MessageCenterType.PERIL -> "隐患类型：${body?.perilCategory ?: "未明确"}"
                MessageCenterType.FAULT -> "故障类型：${body?.faultCategory ?: "未明确"}"
                else -> "关联计划：${body?.planName ?: "--"}"
            }
        }

    val messageThree: String
        get() {
            return when (group) {
                MessageCenterType.APPROVAL,
                MessageCenterType.REPAIR,
                -> {
                    messageType?.let {
                        val approvalType = MessageApprovalType.fromString(it)
                        if (approvalType != MessageApprovalType.UNKNOWN) {
                            when (approvalType) {
                                MessageApprovalType.RETURN_ORDER_NOTIFY_FLOW_REJECTED,
                                MessageApprovalType.RETURN_ORDER_NOTIFY_FLOW_APPROVED,
                                MessageApprovalType.RETURN_ORDER_NOTIFY_FLOW_CC_APPROVED,
                                MessageApprovalType.RETURN_ORDER_NOTIFY_NEXT_NODE,
                                MessageApprovalType.RETURN_ORDER_NOTIFY_FLOW_REVOKED,
                                MessageApprovalType.RETURN_ORDER_NOTIFY_INITIATOR_FLOW_REVOKED,
                                -> {
                                    return "归还日期：${body?.returnTime ?: "--"}"
                                }
                                MessageApprovalType.RECIPIENTS_SOMEONE_REJECTED_ID,
                                MessageApprovalType.RECIPIENTS_ALL_APPROVED_ID,
                                MessageApprovalType.RECIPIENTS_CC_APPROVED_ID,
                                MessageApprovalType.RECIPIENTS_SUBMITTED_ID,
                                MessageApprovalType.RECIPIENTS_REVOKED_ID_APPROVE,
                                MessageApprovalType.RECIPIENTS_REVOKED_ID_APPLICANT,
                                -> {
                                    return "申领日期：${body?.recipientsTime ?: "--"}"
                                }
                                else -> {
                                    val name = if (isPurchaseOrder) "采购数量" else "入库数量"
                                    return "$name：${body?.orderTotalCount ?: "--"}"
                                }
                            }
                        } else {
                            when (MessageRepairTaskType.fromString(it)) {
                                MessageRepairTaskType.REPAIR_TASK_CREATE_ASSIGN,
                                MessageRepairTaskType.REPAIR_TASK_ASSIGN_EXECUTOR,
                                MessageRepairTaskType.REPAIR_TASK_UPDATE_EXECUTOR,
                                -> {
                                    return "开始时间：${body?.startTime ?: "--"}"
                                }
                                MessageRepairTaskType.REPAIR_TASK_CANCEL -> {
                                    return "操作人：${body?.operator ?: "--"}"
                                }
                                MessageRepairTaskType.REPAIR_TASK_START -> {
                                    return "任务周期：${body?.timeCycle ?: "--"}"
                                }
                                MessageRepairTaskType.REPAIR_APPROVAL_PLAN_APPROVAL,
                                MessageRepairTaskType.REPAIR_APPROVAL_PLAN_REVOKE_OTHER,
                                MessageRepairTaskType.REPAIR_APPROVAL_PLAN_REVOKE_APPLICANT,
                                MessageRepairTaskType.REPAIR_APPROVAL_PLAN_APPROVAL_PASS,
                                MessageRepairTaskType.REPAIR_APPROVAL_PLAN_APPROVAL_PASS_CC,
                                MessageRepairTaskType.REPAIR_APPROVAL_PLAN_APPROVAL_REFUSE,
                                -> {
                                    return "是否停机：${body?.isShutdown ?: "--"}"
                                }
                                else -> {
                                    return "设备名称：${body?.deviceName ?: "--"}"
                                }
                            }
                        }
                    }
                    ""
                }
                //出库
                MessageCenterType.WAREHOUSE_OUT -> {
                    return "领用日期：${body?.recipientsTime ?: "--"}"
                }
                MessageCenterType.PERIL -> "隐患描述：${body?.perilDetail ?: "--"}"
                MessageCenterType.FAULT -> "故障描述：${body?.faultDetail ?: "--"}"
                else -> {
                    var msg = ""
                    messageType?.let {
                        msg = if (it.contains(MessageTaskStateType.TASK_START.value)) {
                            "任务周期：${body?.timeCycle ?: "--"}"
                        } else if (it.contains(MessageTaskStateType.TASK_CANCEL.value)) {
                            "操作人：${body?.operator ?: "--"}"
                        } else if (it.contains(MessageTaskStateType.TASK_END_OF_10.value)) {
                            "结束倒计时：${body?.timeDiff ?: "--"}"
                        } else if (it.contains(MessageTaskStateType.TASK_COMPLETE.value)) {
                            "执行人：${body?.executor ?: "--"}"
                        } else {
                            "开始时间：${body?.startTime ?: "--"}"
                        }
                    }
                    return msg
                }
            }
        }
}

data class MessageBodyData(

    //入库时间
    @Json(name = "warehouse_time")
    val warehouseTime:String?,
    //归还时间
    @Json(name="return_time")
    val returnTime:String?,
    //申领单位
    @Json(name="apply_department")
    val applyDepartment:String?,
    //申请时间
    @Json(name = "apply_timestamp")
    val timeStamp: String?,
    //领用日期
    @Json(name = "recipients_time")
    val recipientsTime:String?,
    //设备名称
    @Json(name = "device_name")
    val deviceName: String?,
    //检修计划名称
    @Json(name = "plan_name")
    val planName: String?,
    //检修计划类型
    @Json(name = "repair_type")
    val repairType: String?,
    @Json(name = "start_time")
    val startTime: String?,
    @Json(name = "time_diff")
    val timeDiff: String?,
    //任务执行周期
    @Json(name = "time_cycle")
    val timeCycle: String?,
    //操作人
    @Json(name = "operator")
    val operator: String?,
    //执行人
    @Json(name = "executor")
    val executor: String?,
    //任务 id
    @Json(name = "task_id")
    val taskId: String?,
    //计划 id
    @Json(name = "plan_id")
    val planId: String?,
    //组 id
    @Json(name = "group_id")
    val groupId: String?,
    //隐患类型
    @Json(name = "peril_category")
    val perilCategory: String?,
    //隐患描述
    @Json(name = "peril_detail")
    val perilDetail: String?,
    //故障类型
    @Json(name = "fault_category")
    val faultCategory: String?,
    //故障描述
    @Json(name = "fault_detail")
    val faultDetail: String?,
    //隐患故障ID
    @Json(name = "damage_detail_id")
    val damageDetailId: String?,
    /*采购*/
    //采购ID
    @Json(name = "order_id")
    val orderId: String?,
    //采购单名称
    @Json(name = "order_name")
    val orderName: String?,
    //入库采购单名称
    @Json(name = "purchase_order_name")
    val purchaseOrderName: String?,

    //采购计划类型
    @Json(name = "order_biz_type")
    val orderBizType: String?,
    //采购数量||入库数量
    @Json(name = "order_total_count")
    val orderTotalCount: Int?,
    //入库区
    @Json(name = "warehouse_zone")
    val warehouseZone: String?,
    @Json(name = "order_type")
    val orderType: Int?,
    //是否停机
    @Json(name = "is_shutdown")
    val isShutdown: String?,

    ) {
    val partsOrder: Boolean
        get() {
            return orderBizType == "备件"
        }
    val planType: String?
        get() {
            //年度 1 月度2 季度3
            return orderType?.let {
                when (it) {
                    1 -> return "年度计划"
                    2 -> return "月度计划"
                    3 -> return "季度计划"
                    else -> return ""
                }
            }
        }
}

data class MessageListQueryData(
    @Json(name = "start_time") var startTime: String? = null,
    @Json(name = "end_time") var endTime: String? = null,
    //消息状态 0未读，1已读，多个用逗号隔开
    @Json(name = "status") var status: String? = null,
    @Json(name = "search") var search: String? = null,
    @Json(name = "message_type") var messageType: String? = null,
    @Json(name = "group") var group: MessageCenterType? = null,
)